module("ajax call test");

var flags = {
	"ONEWAY_MESSAGE_FLAG" : 1,
	"TWOWAY_MESSAGE_FLAG" : 2,
	"MY_POST_FLAG" : 4,
	"FRIEND_POST_FLAG" : 8,
	"MY_COMMENT_ON_STATUS_FLAG" : 16,
	"MY_COMMENT_ON_LINK_FLAG" : 32,
	"MY_COMMENT_ON_NOTE_FLAG" : 64,
	"MY_COMMENT_ON_PHOTO_FLAG" : 128,
	"FRIEND_COMMENT_ON_STATUS_FLAG" : 256,
	"FRIEND_COMMENT_ON_LINK_FLAG" : 512,
	"FRIEND_COMMENT_ON_NOTE_FLAG" : 1024,
	"FRIEND_COMMENT_ON_PHOTO_FLAG" : 2048,
	"MY_LIKE_ON_STATUS_FLAG" : 4096,
	"MY_LIKE_ON_LINK_FLAG" : 8192,
	"MY_LIKE_ON_NOTE_FLAG" : 16384,
	"MY_LIKE_ON_PHOTO_FLAG" : 32768,
	"FRIEND_LIKE_ON_STATUS_FLAG" : 65536,
	"FRIEND_LIKE_ON_LINK_FLAG" : 131072,
	"FRIEND_LIKE_ON_NOTE_FLAG" : 262144,
	"FRIEND_LIKE_ON_PHOTO_FLAG" : 524288
};

function getAllFlags(){
	var val = 0;
	for(var i = 0; i < 20; i++){
		val += Math.pow(2,i);
	}
	return val;
}

function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
}

function fetchData(mode, callbackFn){
	$.getJSON('/o-my-friends/ajax/getFriendsDataAJAX.php?mode='+mode, null, function(response){
		if(callbackFn != null && callbackFn != undefined){
			callbackFn.call(this, response);
		}
	});
}

function assertion(flag, response){
	
	if(response.error != "No session exist"){
			
		ok(true, "Fetching data success, " + flag + ":" + flags[flag]);
		ok(response.data != null, "data returned is not null");
		ok(response.mode != null, "must have mode");
		ok(response.data instanceof Array, "data is an array");
		ok(response.error == null, "There MUST BE NO ERROR");
		
		//if response.data is not null there must be one of message flag count
		if(response.data.length > 0){
			var result = false;
			$(response.data).each(function(index, element){
				if(element.count[flag] != undefined){
					result = true;
				}
			});
			ok(result,"There must be more than one data with the flag");
		}
	} else {
		ok(false, "no session");
	}
}

//Friend data fetching test
test("get friend data test", function() {
	stop();

	$.getJSON('/o-my-friends/ajax/getFriendsAJAX.php', null, function(response){
		
		if(response.error != "No session exist"){

			ok(true, "Fetching friend data success");
			ok(response != null, "Return data MUST NOT NULL");
			ok(response.error == null, "There MUST NOT BE AN ERROR");
			
			equal(response.type,"friend");
			ok(response.data != null, "Return data != NULL");
			ok(response.data.length >0, "Number of Friends > 0");
			
		} else {
			ok(false, "no session");
		}
		
		start();
	});
});

//Error Handling : 
test("call without queryString", function(){
	stop();

	$.getJSON('/o-my-friends/ajax/getFriendsDataAJAX.php', null, function(response){
		
		if(response.error != "No session exist"){
			ok(true, "Fetching data success");
			ok(response != null, "Return data MUST NOT NULL");
			ok(response.error != null, "There MUST BE AN ERROR");
		}else{
			ok(false, "no session");
		}
		start();
	});
});

test("call with undefined mode", function(){
	stop();
	fetchData(undefined, function(response){
		if(response.error != "No session exist"){
			ok(true, "Fetching data success");
			ok(response != null, "data returned is not null")
			ok(response.error != null, "There MUST BE AN ERROR");
		}else{
			ok(false, "no session");
		}
		start();
	});

});

test("call with null mode", function(){
	stop();
	fetchData(null, function(response){
		if(response.error != "No session exist"){
			ok(true, "Fetching data success");
			ok(response != null, "data returned is not null")
			ok(response.error != null, "There MUST BE AN ERROR");
		}else{
			ok(false, "no session");
		}
		start();
	});
});

////Exceed time limit
//test("exceed time limit test", function(){
//
//	stop();
//
//	$.getJSON('/o-my-friends/ajax/getFriendsDataAJAX.php?mode=0', null, function(response){
//
//		start();
//	});
//});

//Message data fetching test
test("get message data test", function() {
	stop();
	var flag1 = 'ONEWAY_MESSAGE_FLAG';
	var flag2 = 'TWOWAY_MESSAGE_FLAG';
	fetchData(flags[flag1] + flags[flag2], function(response){
		if(response.error == "No session exist"){
			ok(false, "no session");
		}else{
			ok(true, "Fetching data success, message");
			ok(response.data != null, "data returned is not null");
			ok(response.data instanceof Array, "data is an array");
			ok(response.error == null, "There MUST BE NO ERROR");
			
			//if response.data is not null there must be one of message flag count
			if(response.data.length > 0){
				var result = false;
				$(response.data).each(function(index, element){
					if(element.count[flag1] != undefined || element.count[flag2] != undefined){
						result = true;
					}
				});
				ok(result,"There must be more than one data with one of those message flags");
			}
		}
		start();
		
	});
});

//Post data fetching test
//	My Post
test("get my post data test", function() {
	stop();
	var flag = 'MY_POST_FLAG';
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
//	Friend Post
test("get friend post data test", function() {
	stop();
	var flag = 'FRIEND_POST_FLAG';
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});

//Comment data fetching test
test("get my comment on status data test", function() {
	stop();
	var flag = 'MY_COMMENT_ON_STATUS_FLAG';
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get my comment on link data test", function() {
	stop();
	var flag = "MY_COMMENT_ON_LINK_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get my comment on note data test", function() {
	stop();
	flag = "MY_COMMENT_ON_NOTE_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get my comment on photo data test", function() {
	stop();
	flag = "MY_COMMENT_ON_PHOTO_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get friend comment on status data test", function() {
	stop();
	flag = "FRIEND_COMMENT_ON_STATUS_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get friend comment link data test", function() {
	stop();
	flag = "FRIEND_COMMENT_ON_LINK_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get friend comment note data test", function() {
	stop();
	flag = "FRIEND_COMMENT_ON_NOTE_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get friend comment photo data test", function() {
	stop();
	flag = "FRIEND_COMMENT_ON_PHOTO_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});

//Like data fetching test
test("get my like status data test", function() {
	stop();
	flag = "MY_LIKE_ON_STATUS_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get my like link data test", function() {
	stop();
	flag = "MY_LIKE_ON_LINK_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get my like note data test", function() {
	stop();
	flag = "MY_LIKE_ON_NOTE_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get my like photo data test", function() {
	stop();
	flag = "MY_LIKE_ON_PHOTO_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});

test("get friend like status data test", function() {
	stop();
	flag = "FRIEND_LIKE_ON_STATUS_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get friend like link data test", function() {
	stop();
	flag = "FRIEND_LIKE_ON_LINK_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get friend like note data test", function() {
	stop();
	flag = "FRIEND_LIKE_ON_NOTE_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});
test("get friend like photo data test", function() {
	stop();
	flag = "FRIEND_LIKE_ON_PHOTO_FLAG";
	fetchData(flags[flag], function(response){
		assertion(flag, response);
		start();
	});
});

test("get all data at once", function(){
	stop();
	flag = getAllFlags();
	fetchData(flag, function(response){
		start();
		ok(response.data != null);
		
	});
});

test("get message data without error", function(){
	stop();
	mode = flags.ONEWAY_MESSAGE_FLAG + flags.TWOWAY_MESSAGE_FLAG;
	fetchData(mode, function(response){
		start();
		ok(response.data != null, "mode:"+mode);
		
	});
});

test("get post data without error", function(){
	stop();
	mode = flags.MY_POST_FLAG + flags.FRIEND_POST_FLAG;
	fetchData(mode, function(response){
		start();
		ok(response.data != null, "mode:"+mode);
	});
});
test("get comment data without error", function(){
	stop();
	mode = flags.FRIEND_COMMENT_ON_LINK_FLAG + flags.FRIEND_COMMENT_ON_NOTE_FLAG + flags.FRIEND_COMMENT_ON_PHOTO_FLAG + flags.FRIEND_COMMENT_ON_STATUS_FLAG
			+ flags.MY_COMMENT_ON_LINK_FLAG + flags.MY_COMMENT_ON_NOTE_FLAG + flags.MY_COMMENT_ON_PHOTO_FLAG + flags.MY_COMMENT_ON_STATUS_FLAG;
	fetchData(mode, function(response){
		start();
		ok(response.data != null, "mode:"+mode);
	});
});
test("get like data without error", function(){
	stop();
	mode = flags.FRIEND_LIKE_ON_LINK_FLAG + flags.FRIEND_LIKE_ON_NOTE_FLAG + flags.FRIEND_LIKE_ON_PHOTO_FLAG + flags.FRIEND_LIKE_ON_STATUS_FLAG
			+ flags.MY_LIKE_ON_LINK_FLAG + flags.MY_LIKE_ON_NOTE_FLAG + flags.MY_LIKE_ON_PHOTO_FLAG + flags.MY_LIKE_ON_STATUS_FLAG;
	fetchData(mode, function(response){
		start();
		ok(response.data != null, "mode:"+mode);
	});
});

test("get post data wit an error with my post", function(){
	
});